#include "joystickreader.h"
#include <QDebug>
#include <QTimer>
#include <QString>

#include "Utils.h"

JoystickReader::JoystickReader(QObject *parent) : QObject(parent), joystick(nullptr) {
    // 初始化 HIDAPI 库
    if (hid_init()) {
        qCritical() << "Failed to initialize HIDAPI library.";
    }

    // 枚举所有连接的 HID 设备
    struct hid_device_info *devs, *cur_dev, *cur_dev2;
    devs = hid_enumerate(0x0, 0x0);
    cur_dev = devs;
    cur_dev2 = devs;

    // 遍历所有HID设备，找到生产商名为"Thustmaster"的设备，为摇杆
    while (cur_dev) {
        qDebug() << QString::fromWCharArray(cur_dev->manufacturer_string);
        if (QString::fromWCharArray(cur_dev->manufacturer_string).contains("Thustmaster")) {
            joystick = hid_open_path(cur_dev->path);
            break;
        }
        cur_dev = cur_dev->next;
    }

    // 遍历所有HID设备，找到生产商名为"Thrustmaster"的设备，为推杆
    while(cur_dev2) {
        if (QString::fromWCharArray(cur_dev2->manufacturer_string).contains("Thrustmaster")) {
            joystick2 = hid_open_path(cur_dev2->path);
            break;
        }
        cur_dev2 = cur_dev2->next;
    }
    hid_free_enumeration(devs);
}

JoystickReader::~JoystickReader() {
    if (joystick) {
        hid_close(joystick);
    }
    hid_exit();
}

void JoystickReader::readJoystickData() {
    // 读取摇杆数据
    if (joystick) {
        unsigned char buf[65];
        memset(buf, 0, sizeof(buf));

        // 读取数据
        int res = hid_read(joystick, buf, sizeof(buf));
        if (res > 0) {
            // printRawData(buf, res, 16);
            parse1Buttons(buf, res);
            parse1Triggers(buf, res);
            parse1Axis4(buf, res);
            parse1Axis5(buf, res);
            parse1Axis81(buf, res);
            parse1Axis82(buf, res);
            parse1Rocker(buf,res);
        }
    } else {
        qCritical() << "No Thustmaster joystick device found.";
    }

    // 读取控制台数据
    if(joystick2) {
        unsigned char buf[65];
        memset(buf, 0, sizeof(buf));

        int res = hid_read(joystick2, buf, sizeof(buf));
        if (res > 0) {
            printRawData(buf, res, 16);
            parse2Buttons(buf, res);
            parse2Switcher2s(buf, res);
            parse2Switcher3s(buf, res);
            parse2Axis4(buf, res);
            parse2Axis8(buf, res);
            parse2Slider(buf, res);
            parse2AxisSlider(buf, res);
            parse2Pushers(buf, res);
        }
    } else {
        qCritical() << "No Thrustmaster joystick device found.";
    }
}

/*
 * data :   字节数组
 * n    :   字节个数
 * base :   进制
*/
void JoystickReader::printRawData(unsigned char data[], int n, int base)
{
    QString print;
    for (int i = 0; i < n; ++i) {
        switch(base) {
        case 2:
            print.append(QString::number(data[i], 2).rightJustified(8, '0') + "\n");
            break;
        case 10:
            print.append(QString::number(data[i], 10).rightJustified(3, '0') + " ");
            break;
        case 16:
            print.append(QString::number(data[i], 16).rightJustified(2, '0') + " ");
            break;
        }
    }
    emit joystickDataReceived(print);
}

/*
 * data :   字节数组
 * n    :   字节个数
*/
void JoystickReader::parse1Buttons(unsigned char data[], int n)
{
    if(n > 2) {
        quint8 secondByte = data[1];
        if((secondByte & 0x04) >> 2)
            setButton1(JoystickReader::Button_PUSHED);
        else
            setButton1(JoystickReader::Button_RELEASE);

        if((secondByte & 0x02) >> 1)
            setButton2(JoystickReader::Button_PUSHED);
        else
            setButton2(JoystickReader::Button_RELEASE);

        if((secondByte & 0x10) >> 4)
            setButton3(JoystickReader::Button_PUSHED);
        else
            setButton3(JoystickReader::Button_RELEASE);
    }
}

void JoystickReader::parse1Triggers(unsigned char data[], int n)
{
    if(n > 2) {
        quint8 secondByte = data[1];
        if((secondByte & 0x08) >> 3)
            setTrigger1(JoystickReader::Trigger_PUSHED);
        else
            setTrigger1(JoystickReader::Trigger_RELEASE);

        if(secondByte & 0x01)
            setTrigger2(JoystickReader::Trigger_PUSHED);
        else
            setTrigger2(JoystickReader::Trigger_RELEASE);
    }
}

void JoystickReader::parse1Axis4(unsigned char data[], int n)
{
    if(n > 4) {
        quint8 fourthByte = data[3];
        switch (fourthByte) {
        case 0x00:
            setAxis4(JoystickReader::Axis_UP);
            break;
        case 0x20:
            setAxis4(JoystickReader::Axis_RIGHT);
            break;
        case 0x40:
            setAxis4(JoystickReader::Axis_DOWN);
            break;
        case 0x60:
            setAxis4(JoystickReader::Axis_LEFT);
            break;
        case 0xf0:
            setAxis4(JoystickReader::Axis_RELEASE);
            break;
        }
    }
}

void JoystickReader::parse1Axis5(unsigned char data[], int n)
{
    if(n > 4) {
        quint8 thirdByte = data[2], fourthByte = data[3];

        int tag = 0;    // 标志位，如果解析axis5完成后，tag还是0，则将其设置成release
        if((thirdByte & 0x40) >> 6) {
            setAxis5(JoystickReader::Axis_UP);
            tag = 1;
        }

        if((thirdByte & 0x80) >> 7) {
            setAxis5(JoystickReader::Axis_RIGHT);
            tag = 1;
        }

        switch (fourthByte) {
        case 0xf1:
            setAxis5(JoystickReader::Axis_DOWN);
            tag = 1;
            break;
        case 0xf2:
            setAxis5(JoystickReader::Axis_LEFT);
            tag = 1;
            break;
        case 0xf4:
            setAxis5(JoystickReader::Axis_PRESSED);
            tag = 1;
            break;
        }

        if(!tag)
            setAxis5(JoystickReader::Axis_RELEASE);
    }
}

void JoystickReader::parse1Axis81(unsigned char data[], int n) {
    if(n > 3) {
        quint8 thirdByte = data[2];

        switch(thirdByte) {
        case 0x04:
            setAxis81(JoystickReader::Axis_UP);
            break;
        case 0x10:
            setAxis81(JoystickReader::Axis_DOWN);
            break;
        case 0x20:
            setAxis81(JoystickReader::Axis_LEFT);
            break;
        case 0x08:
            setAxis81(JoystickReader::Axis_RIGHT);
            break;
        case 0x24:
            setAxis81(JoystickReader::Axis_LEFT_UP);
            break;
        case 0x30:
            setAxis81(JoystickReader::Axis_LEFT_DOWN);
            break;
        case 0x0c:
            setAxis81(JoystickReader::Axis_RIGHT_UP);
            break;
        case 0x18:
            setAxis81(JoystickReader::Axis_RIGHT_DOWN);
            break;
        default:
            setAxis81(JoystickReader::Axis_RELEASE);
        }
    }
}

void JoystickReader::parse1Axis82(unsigned char data[], int n)
{
    if(n > 3) {
        quint8 secondByte = data[1], thirdByte = data[2];

        int tag = 0;

        switch(secondByte) {
        case 0x40:
            setAxis82(JoystickReader::Axis_UP);
            tag = 1;
            break;
        case 0x80:
            setAxis82(JoystickReader::Axis_RIGHT);
            tag = 1;
            break;
        case 0xc0:
            setAxis82(JoystickReader::Axis_RIGHT_UP);
            tag = 1;
            break;
        }

        switch(thirdByte) {
        case 0x01:
            setAxis82(JoystickReader::Axis_DOWN);
            tag = 1;
            break;
        case 0x02:
            setAxis82(JoystickReader::Axis_LEFT);
            tag = 1;
            break;
        case 0x03:
            setAxis82(JoystickReader::Axis_LEFT_DOWN);
            tag = 1;
            break;
        }

        if(secondByte == 0x40 && thirdByte == 0x02) {
            setAxis82(JoystickReader::Axis_LEFT_UP);
            tag = 1;
        }

        if(secondByte == 0x80 && thirdByte == 0x01) {
            setAxis82(JoystickReader::Axis_RIGHT_DOWN);
            tag = 1;
        }

        if(!tag)
            setAxis82(JoystickReader::Axis_RELEASE);
    }
}

void JoystickReader::parse1Rocker(unsigned char data[], int n)
{
    // 横向
    if(n > 6) {
        quint8 sixthByte = data[5];
        setRockerX(Utils::normalization(sixthByte, 0xff, 0x00, 0.02));
    }

    // 纵向
    if(n > 8) {
        quint8 eighthByte = data[7];
        setRockerY(Utils::normalization(eighthByte, 0xff, 0x00, 0.02));
    }
}

void JoystickReader::parse2Buttons(unsigned char data[], int n)
{
    if(n > 5) {
        quint8 thirdByte = data[2], fourthByte = data[3], fifthByte = data[4];

        if((fifthByte & 0x02) >> 1)
            setButton21(JoystickReader::Button_PUSHED);
        else
            setButton21(JoystickReader::Button_RELEASE);

        if((fourthByte & 0x10) >> 4)
            setButton22(JoystickReader::Button_PUSHED);
        else
            setButton22(JoystickReader::Button_RELEASE);

        if((thirdByte & 0x40) >> 6)
            setButton23(JoystickReader::Button_PUSHED);
        else
            setButton23(JoystickReader::Button_RELEASE);
    }
}

void JoystickReader::parse2Switcher2s(unsigned char data[], int n)
{
    if(n > 5) {
        quint8 thirdByte = data[2], fourthByte = data[3], fifthByte = data[4];

        if((fourthByte & 0x80) >> 7)
            setSwitcher21(JoystickReader::Switcher2_ON);
        else
            setSwitcher21(JoystickReader::Switcher2_OFF);

        if((fifthByte & 0x01) >> 0)
            setSwitcher22(JoystickReader::Switcher2_ON);
        else
            setSwitcher22(JoystickReader::Switcher2_OFF);

        if((fourthByte & 0x08) >> 3)
            setSwitcher23(JoystickReader::Switcher2_ON);
        else
            setSwitcher23(JoystickReader::Switcher2_OFF);

        if((thirdByte & 0x80) >> 7)
            setSwitcher24(JoystickReader::Switcher2_ON);
        else
            setSwitcher24(JoystickReader::Switcher2_OFF);

        if((fourthByte & 0x01) >> 0)
            setSwitcher25(JoystickReader::Switcher2_ON);
        else
            setSwitcher25(JoystickReader::Switcher2_OFF);
    }
}

void JoystickReader::parse2Switcher3s(unsigned char data[], int n)
{
    if(n > 5) {
        quint8 secondByte = data[1], thirdByte = data[2], fourthByte = data[3], fifthByte = data[4];

        if((fifthByte & 0x04) >> 2)
            setSwitcher31(JoystickReader::Switcher3_UP);
        else if((fifthByte & 0x08) >> 3)
            setSwitcher31(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher31(JoystickReader::Switcher3_MID);

        if((fourthByte & 0x20) >> 5)
            setSwitcher32(JoystickReader::Switcher3_UP);
        else if((fourthByte & 0x40) >> 6)
            setSwitcher32(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher32(JoystickReader::Switcher3_MID);

        if((fifthByte & 0x80) >> 7)
            setSwitcher33(JoystickReader::Switcher3_UP);
        else if((fourthByte & 0x04) >> 2)
            setSwitcher33(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher33(JoystickReader::Switcher3_MID);

        if((fifthByte & 0x40) >> 6)
            setSwitcher34(JoystickReader::Switcher3_UP);
        else if((fourthByte & 0x02) >> 1)
            setSwitcher34(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher34(JoystickReader::Switcher3_MID);

        if((thirdByte & 0x10) >> 4)
            setSwitcher35(JoystickReader::Switcher3_UP);
        else if((thirdByte & 0x20) >> 5)
            setSwitcher35(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher35(JoystickReader::Switcher3_MID);

        if((secondByte & 0x40) >> 6)
            setSwitcher36(JoystickReader::Switcher3_UP);
        else if((secondByte & 0x80) >> 7)
            setSwitcher36(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher36(JoystickReader::Switcher3_MID);

        if((thirdByte & 0x01))
            setSwitcher37(JoystickReader::Switcher3_UP);
        else if((thirdByte & 0x02) >> 1)
            setSwitcher37(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher37(JoystickReader::Switcher3_MID);

        if((thirdByte & 0x04) >> 2)
            setSwitcher38(JoystickReader::Switcher3_UP);
        else if((thirdByte & 0x08) >> 3)
            setSwitcher38(JoystickReader::Switcher3_DOWN);
        else
            setSwitcher38(JoystickReader::Switcher3_MID);
    }
}

void JoystickReader::parse2Axis4(unsigned char data[], int n)
{
    if(n > 2) {
        quint8 secondByte = data[1];

        switch (secondByte) {
        case 0x04:
            setAxis42(JoystickReader::Axis_UP);
            break;
        case 0x10:
            setAxis42(JoystickReader::Axis_DOWN);
            break;
        case 0x20:
            setAxis42(JoystickReader::Axis_LEFT);
            break;
        case 0x08:
            setAxis42(JoystickReader::Axis_RIGHT);
            break;
        default:
            setAxis42(JoystickReader::Axis_RELEASE);
        }
    }
}

void JoystickReader::parse2Axis8(unsigned char data[], int n)
{
    if(n > 2) {
        quint8 sixthByte = data[5];

        switch (sixthByte) {
        case 0x44:
            setAxis8(JoystickReader::Axis_UP);
            break;
        case 0x10:
            setAxis8(JoystickReader::Axis_DOWN);
            break;
        case 0x86:
            setAxis8(JoystickReader::Axis_LEFT);
            break;
        case 0x22:
            setAxis8(JoystickReader::Axis_RIGHT);
            break;
        case 0xc5:
            setAxis8(JoystickReader::Axis_LEFT_UP);
            break;
        case 0x97:
            setAxis8(JoystickReader::Axis_LEFT_DOWN);
            break;
        case 0x63:
            setAxis8(JoystickReader::Axis_RIGHT_UP);
            break;
        case 0x31:
            setAxis8(JoystickReader::Axis_RIGHT_DOWN);
            break;
        case 0x0f:
            setAxis8(JoystickReader::Axis_RELEASE);
        }
    }
}

void JoystickReader::parse2AxisSlider(unsigned char data[], int n)
{
    if(n > 10) {
        quint8 secondByte = data[1], seventhByte = data[6], eighthByte = data[7], ninthByte = data[8], tenthByte = data[9];

        if((secondByte & 0x01) >> 0)
            setAxisSliderPressed(true);
        else
            setAxisSliderPressed(false);

        int value1 = seventhByte + 0xff * eighthByte;
        int value2 = ninthByte + 0xff * tenthByte;
        int min = 0, max = 0xff * 4;
        setAxisSliderX(Utils::normalization(value1, max, min, 0.05));
        setAxisSliderY(Utils::normalization(value2, max, min, 0.05));
    }
}

void JoystickReader::parse2Slider(unsigned char data[], int n)
{
    if(n > 12) {
        quint8 eleventhByte = data[10], twelfthByte = data[11];

        int value = eleventhByte + 0xff * twelfthByte;
        int min = 0, max = 0xff * 4;
        setSlider(Utils::normalization(value, max, min, 0.01));
    }
}

void JoystickReader::parse2Pushers(unsigned char data[], int n)
{
    if(n > 16) {
        quint8 thirteenthByte = data[12], fourteenthByte = data[13];
        quint8 fifteenthByte = data[14], sixteenthByte = data[15];

        int value1 = fifteenthByte + 0xff * sixteenthByte;
        int value2 = thirteenthByte + 0xff * fourteenthByte;
        int min = 0, max = 0xff * 0x40;
        setPusher1(Utils::normalization(value1, max, min, 0.05));
        setPusher2(Utils::normalization(value2, max, min, 0.05));
    }
}



JoystickReader::ButtonState JoystickReader::button1() const
{
    return m_button1;
}

void JoystickReader::setButton1(ButtonState newButton1)
{
    if (m_button1 == newButton1)
        return;
    m_button1 = newButton1;
    emit button1Changed();
}

JoystickReader::ButtonState JoystickReader::button2() const
{
    return m_button2;
}

void JoystickReader::setButton2(ButtonState newButton2)
{
    if (m_button2 == newButton2)
        return;
    m_button2 = newButton2;
    emit button2Changed();
}

JoystickReader::ButtonState JoystickReader::button3() const
{
    return m_button3;
}

void JoystickReader::setButton3(ButtonState newButton3)
{
    if (m_button3 == newButton3)
        return;
    m_button3 = newButton3;
    emit button3Changed();
}

JoystickReader::TriggerState JoystickReader::trigger1() const
{
    return m_trigger1;
}

void JoystickReader::setTrigger1(TriggerState newTrigger1)
{
    if (m_trigger1 == newTrigger1)
        return;
    m_trigger1 = newTrigger1;
    emit trigger1Changed();
}

JoystickReader::TriggerState JoystickReader::trigger2() const
{
    return m_trigger2;
}

void JoystickReader::setTrigger2(TriggerState newTrigger2)
{
    if (m_trigger2 == newTrigger2)
        return;
    m_trigger2 = newTrigger2;
    emit trigger2Changed();
}

JoystickReader::AxisState JoystickReader::axis4() const
{
    return m_axis4;
}

void JoystickReader::setAxis4(AxisState newAxis4)
{
    if (m_axis4 == newAxis4)
        return;
    m_axis4 = newAxis4;
    emit axis4Changed();
}

JoystickReader::AxisState JoystickReader::axis5() const
{
    return m_axis5;
}

void JoystickReader::setAxis5(AxisState newAxis5)
{
    if (m_axis5 == newAxis5)
        return;
    m_axis5 = newAxis5;
    emit axis5Changed();
}

JoystickReader::AxisState JoystickReader::axis81() const
{
    return m_axis81;
}

void JoystickReader::setAxis81(AxisState newAxis81)
{
    if (m_axis81 == newAxis81)
        return;
    m_axis81 = newAxis81;
    emit axis81Changed();
}

JoystickReader::AxisState JoystickReader::axis82() const
{
    return m_axis82;
}

void JoystickReader::setAxis82(AxisState newAxis82)
{
    if (m_axis82 == newAxis82)
        return;
    m_axis82 = newAxis82;
    emit axis82Changed();
}

double JoystickReader::rockerX() const
{
    return m_rockerX;
}

void JoystickReader::setRockerX(double newRockerX)
{
    if (qFuzzyCompare(m_rockerX, newRockerX))
        return;
    m_rockerX = newRockerX;
    emit rockerXChanged();
}

double JoystickReader::rockerY() const
{
    return m_rockerY;
}

void JoystickReader::setRockerY(double newRockerY)
{
    if (qFuzzyCompare(m_rockerY, newRockerY))
        return;
    m_rockerY = newRockerY;
    emit rockerYChanged();
}

JoystickReader::ButtonState JoystickReader::button21() const
{
    return m_button21;
}

void JoystickReader::setButton21(ButtonState newButton21)
{
    if (m_button21 == newButton21)
        return;
    m_button21 = newButton21;
    emit button21Changed();
}

JoystickReader::ButtonState JoystickReader::button22() const
{
    return m_button22;
}

void JoystickReader::setButton22(ButtonState newButton22)
{
    if (m_button22 == newButton22)
        return;
    m_button22 = newButton22;
    emit button22Changed();
}

JoystickReader::ButtonState JoystickReader::button23() const
{
    return m_button23;
}

void JoystickReader::setButton23(ButtonState newButton23)
{
    if (m_button23 == newButton23)
        return;
    m_button23 = newButton23;
    emit button23Changed();
}

JoystickReader::Switcher2State JoystickReader::switcher21() const
{
    return m_switcher21;
}

void JoystickReader::setSwitcher21(Switcher2State newSwitcher21)
{
    if (m_switcher21 == newSwitcher21)
        return;
    m_switcher21 = newSwitcher21;
    emit switcher21Changed();
}

JoystickReader::Switcher2State JoystickReader::switcher22() const
{
    return m_switcher22;
}

void JoystickReader::setSwitcher22(Switcher2State newSwitcher22)
{
    if (m_switcher22 == newSwitcher22)
        return;
    m_switcher22 = newSwitcher22;
    emit switcher22Changed();
}

JoystickReader::Switcher2State JoystickReader::switcher23() const
{
    return m_switcher23;
}

void JoystickReader::setSwitcher23(Switcher2State newSwitcher23)
{
    if (m_switcher23 == newSwitcher23)
        return;
    m_switcher23 = newSwitcher23;
    emit switcher23Changed();
}

JoystickReader::Switcher2State JoystickReader::switcher24() const
{
    return m_switcher24;
}

void JoystickReader::setSwitcher24(Switcher2State newSwitcher24)
{
    if (m_switcher24 == newSwitcher24)
        return;
    m_switcher24 = newSwitcher24;
    emit switcher24Changed();
}

JoystickReader::Switcher2State JoystickReader::switcher25() const
{
    return m_switcher25;
}

void JoystickReader::setSwitcher25(Switcher2State newSwitcher25)
{
    if (m_switcher25 == newSwitcher25)
        return;
    m_switcher25 = newSwitcher25;
    emit switcher25Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher31() const
{
    return m_switcher31;
}

void JoystickReader::setSwitcher31(Switcher3State newSwitcher31)
{
    if (m_switcher31 == newSwitcher31)
        return;
    m_switcher31 = newSwitcher31;
    emit switcher31Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher32() const
{
    return m_switcher32;
}

void JoystickReader::setSwitcher32(Switcher3State newSwitcher32)
{
    if (m_switcher32 == newSwitcher32)
        return;
    m_switcher32 = newSwitcher32;
    emit switcher32Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher33() const
{
    return m_switcher33;
}

void JoystickReader::setSwitcher33(Switcher3State newSwitcher33)
{
    if (m_switcher33 == newSwitcher33)
        return;
    m_switcher33 = newSwitcher33;
    emit switcher33Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher34() const
{
    return m_switcher34;
}

void JoystickReader::setSwitcher34(Switcher3State newSwitcher34)
{
    if (m_switcher34 == newSwitcher34)
        return;
    m_switcher34 = newSwitcher34;
    emit switcher34Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher35() const
{
    return m_switcher35;
}

void JoystickReader::setSwitcher35(Switcher3State newSwitcher35)
{
    if (m_switcher35 == newSwitcher35)
        return;
    m_switcher35 = newSwitcher35;
    emit switcher35Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher36() const
{
    return m_switcher36;
}

void JoystickReader::setSwitcher36(Switcher3State newSwitcher36)
{
    if (m_switcher36 == newSwitcher36)
        return;
    m_switcher36 = newSwitcher36;
    emit switcher36Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher37() const
{
    return m_switcher37;
}

void JoystickReader::setSwitcher37(Switcher3State newSwitcher37)
{
    if (m_switcher37 == newSwitcher37)
        return;
    m_switcher37 = newSwitcher37;
    emit switcher37Changed();
}

JoystickReader::Switcher3State JoystickReader::switcher38() const
{
    return m_switcher38;
}

void JoystickReader::setSwitcher38(Switcher3State newSwitcher38)
{
    if (m_switcher38 == newSwitcher38)
        return;
    m_switcher38 = newSwitcher38;
    emit switcher38Changed();
}

JoystickReader::AxisState JoystickReader::axis8() const
{
    return m_axis8;
}

void JoystickReader::setAxis8(AxisState newAxis8)
{
    if (m_axis8 == newAxis8)
        return;
    m_axis8 = newAxis8;
    emit axis8Changed();
}

double JoystickReader::axisSliderX() const
{
    return m_axisSliderX;
}

void JoystickReader::setAxisSliderX(double newAxisSliderX)
{
    if (qFuzzyCompare(m_axisSliderX, newAxisSliderX))
        return;
    m_axisSliderX = newAxisSliderX;
    emit axisSliderXChanged();
}

double JoystickReader::axisSliderY() const
{
    return m_axisSliderY;
}

void JoystickReader::setAxisSliderY(double newAxisSliderY)
{
    if (qFuzzyCompare(m_axisSliderY, newAxisSliderY))
        return;
    m_axisSliderY = newAxisSliderY;
    emit axisSliderYChanged();
}

JoystickReader::AxisState JoystickReader::axis42() const
{
    return m_axis42;
}

void JoystickReader::setAxis42(AxisState newAxis42)
{
    if (m_axis42 == newAxis42)
        return;
    m_axis42 = newAxis42;
    emit axis42Changed();
}

bool JoystickReader::axisSliderPressed() const
{
    return m_axisSliderPressed;
}

void JoystickReader::setAxisSliderPressed(bool newAxisSliderPressed)
{
    if (m_axisSliderPressed == newAxisSliderPressed)
        return;
    m_axisSliderPressed = newAxisSliderPressed;
    emit axisSliderPressedChanged();
}

double JoystickReader::slider() const
{
    return m_slider;
}

void JoystickReader::setSlider(double newSlider)
{
    if (qFuzzyCompare(m_slider, newSlider))
        return;
    m_slider = newSlider;
    emit sliderChanged();
}

double JoystickReader::pusher1() const
{
    return m_pusher1;
}

void JoystickReader::setPusher1(double newPusher1)
{
    if (qFuzzyCompare(m_pusher1, newPusher1))
        return;
    m_pusher1 = newPusher1;
    emit pusher1Changed();
}

double JoystickReader::pusher2() const
{
    return m_pusher2;
}

void JoystickReader::setPusher2(double newPusher2)
{
    if (qFuzzyCompare(m_pusher2, newPusher2))
        return;
    m_pusher2 = newPusher2;
    emit pusher2Changed();
}
